package comWorld.blog.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import comWorld.blog.BlogInfo;
import comWorld.blog.BlogInfoManage;
import comWorld.blog.BlogInfos;
import comWorld.blogCatalog.BlogCatalog;
import comWorld.blogCatalog.BlogCatalogs;

/**
 * Servlet implementation class EditBlogPageInit
 */
@WebServlet("/admin/edit-blog")
public class EditBlogPageInit extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditBlogPageInit() {
        super();
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int draftId = Integer.valueOf(request.getParameter("draft"));
		BlogInfos draftInfos = (BlogInfos) request.getSession().getAttribute("draftInfos");
		BlogInfo draftInfo = draftInfos.get(draftId);
		request.setAttribute("draftInfo", draftInfo);
		
		BlogCatalogs blogCatalogs = new BlogCatalogs(draftInfos.getUserId());
		BlogCatalog currCatalog = blogCatalogs.get(draftInfo.getCatalogId());
		blogCatalogs.remove(currCatalog);
		blogCatalogs.add(0, currCatalog);
		request.setAttribute("blogCatalogs", blogCatalogs);
		
		String content = BlogInfoManage.getBlogContent(draftInfo.getBlogId(), draftInfo.getUserId(), getServletContext());
		request.setAttribute("content", content);
		
		request.getRequestDispatcher("/blog/editBlog.jsp").forward(request, response);
	}

}
